﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController
{
    /// <summary>
    /// 资料业务逻辑
    /// </summary>
    public class SourceController : ControllerBase
    {
        //本类的单列对象
        private static SourceController _sourceController;

        //单列模式实例化
        public static SourceController getController()
        {
            if (_sourceController == null)
            {
                _sourceController = new SourceController();
            }
            return _sourceController;
        }

        #region 资料

        /// <summary>
        /// 资料数据库访问
        /// </summary>
        private hf.DAL.Source SourceDAL = new hf.DAL.Source();

        /// <summary>
        /// 添加资料
        /// </summary>
        /// <param name="SourceID"></param>
        /// <param name="Title"></param>
        /// <param name="Author"></param>
        /// <param name="ReSource"></param>
        /// <param name="Content"></param>
        public void doAddSource(string SourceID, string Title, string Author,DateTime Addtime, string ReSource, string Content, int CityID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Source model = new hf.Model.Source();

            model.SourceID = int.Parse(SourceID);
            model.Title = Title;
            model.Author = Author;
            model.Addtime = Addtime;
            model.ReSource = ReSource;
            model.Content = Content;
            model.SmallSourceID = 0;
            model.ThirdSourceID = 0;
            model.CityID = CityID;
            try
            {
                model.ID = SourceDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='Source.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch(Exception err)
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 根据ID得到一条资料
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.Source getSourceModel(int ID)
        {
            hf.Model.Source model = new hf.Model.Source();
            int total;
            List<hf.Model.Source> list = SourceDAL.GetSourceList(1, 1, "ID=" + ID.ToString(), out total);

            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改资料
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="SourceID"></param>
        /// <param name="Title"></param>
        /// <param name="Author"></param>
        /// <param name="ReSource"></param>
        /// <param name="Content"></param>
        public void doModifySource(int ID, string SourceID, string Title, string Author,DateTime Addtime, string ReSource, string Content, int CityID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Source model = this.getSourceModel(ID);

            model.SourceID = int.Parse(SourceID);
            model.Title = Title;
            model.Author = Author;
            model.Addtime = Addtime;
            model.ReSource = ReSource;
            model.Content = Content;
            model.SmallSourceID = 0;
            model.ThirdSourceID = 0;
            model.CityID = CityID;

            try
            {
                //执行修改
                SourceDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='SourceList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch(Exception Err)
            {
                result.isTrue = false;
                result.Message = Err.Message;
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 根据大分类ID得到指定数量的记录
        /// </summary>
        /// <param name="SourceID"></param>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<hf.Model.Source> getSourceList(int SourceID ,int Count)
        {
            List<hf.Model.Source> list = new List<hf.Model.Source>();

            //每页显示的记录
            int PageSize = Count;

            //总数据记录
            int total;

            //筛选条件
            string strSql = "SourceID = " + SourceID.ToString();

            //获得列表
            list = SourceDAL.GetSourceList(PageSize, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 前台搜索用
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.Source> getSourceList(int PageSize ,int PageNumber ,string strWhere ,out int total)
        {
            List<hf.Model.Source> list = new List<hf.Model.Source>();
            //筛选条件
            string strSql = "";

            //获得列表
            list = SourceDAL.GetSourceList(PageSize, PageNumber, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到资料列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.Source> getSourceList(out string PagerStr)
        {
            List<hf.Model.Source> list = new List<hf.Model.Source>();

            //每页显示的记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }

            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = SourceDAL.GetSourceList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 删除资料
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeSource(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                SourceDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='Sourcelist.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeSource(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    SourceDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='SourceList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        #endregion

        #region  资料库类别

        /// <summary>
        /// 资料库类别数据访问
        /// </summary>
        private hf.DAL.SourceClass SourceClassDAL = new hf.DAL.SourceClass();

        /// <summary>
        /// 添加资料库类别
        /// </summary>
        /// <param name="ClassName"></param>
        public void doAddSourceClass(string ClassName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.SourceClass model = new hf.Model.SourceClass();

            model.ClassName = ClassName;

            try
            {
                model.ID = SourceClassDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='SourceClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 根据ID得到一条资料库类别记录
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.SourceClass getSourceClassModel(int ID)
        {
            hf.Model.SourceClass model = new hf.Model.SourceClass();
            int total;
            List<hf.Model.SourceClass> list = SourceClassDAL.GetSourceClassList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改资料库类别
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="ClassName"></param>
        public void doModifySourceClass(int ID, string ClassName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            //根据ID 得到一条数据
            // 问题在这里 hf.Model.SourceClass model = new hf.Model.SourceClass();
            //改成这个
            hf.Model.SourceClass model = this.getSourceClassModel(ID);
            //修改这条数据的Name
            model.ClassName = ClassName;

            try
            {
                //执行修改
                SourceClassDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='SourceClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到资料列表，用作绑定下拉框（方法名和下面的一样，这个是重载）
        /// </summary>
        /// <param name="LoadCount">读取记录数</param>
        /// <returns></returns>
        public List<hf.Model.SourceClass> getSourceClassList(int LoadCount)
        {
            List<hf.Model.SourceClass> list = new List<hf.Model.SourceClass>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = SourceClassDAL.GetSourceClassList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到资料列库类别表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.SourceClass> getSourceClassList(out string PagerStr)
        {
            List<hf.Model.SourceClass> list = new List<hf.Model.SourceClass>();

            //每页显示的记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }

            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = SourceClassDAL.GetSourceClassList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 删除资料库类别
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeSourceClass(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                SourceClassDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='SourceClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        } 
        #endregion

        #region 资料库二级类别
        /// <summary>
        /// 资料库类别数据访问
        /// </summary>
        private hf.DAL.SmallSourceDAL sClassDAL = new hf.DAL.SmallSourceDAL();

        /// <summary>
        /// 根据一级分类，得到列表（前台选择菜单用）
        /// </summary>
        /// <param name="SourceID"></param>
        /// <param name="LoadCount"></param>
        /// <returns></returns>
        public List<hf.Model.SmallSourceModel> getSmallSourceClassList(int SourceID , int LoadCount)
        {
            List<hf.Model.SmallSourceModel> list = new List<hf.Model.SmallSourceModel>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "SourceID = " + SourceID.ToString();

            //获得列表
            list = sClassDAL.GetThirdSourceList(LoadCount, 1, strSql, out total);

            return list;
        }
        #endregion

        #region 资料库三级类别
        /// <summary>
        /// 
        /// </summary>
        private hf.DAL.ThirdSourceDAL tClassDAL = new hf.DAL.ThirdSourceDAL();
        /// <summary>
        /// 根据资料库二级类别，得到三级类别的列表（用作前台选择菜单）
        /// </summary>
        /// <param name="SmallSourceID"></param>
        /// <param name="LoadCount"></param>
        /// <returns></returns>
        public List<hf.Model.ThirdSourceModel> getThirdSourceClassList(int SmallSourceID, int LoadCount)
        {
            List<hf.Model.ThirdSourceModel> list = new List<hf.Model.ThirdSourceModel>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "SmallSourceID = " + SmallSourceID.ToString();

            //获得列表
            list = tClassDAL.GetThirdSourceList(LoadCount, 1, strSql, out total);

            return list;
        }
        #endregion
    }
}

